package com.ccq.farm.shiro;/**
 * Created by HiWin10 on 2018-07-01.
 */

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author ccq
 * @create 2018-07-01 下午 14:15
 **/
@Controller
public class HomePageController {


    @RequestMapping("ec")
    public String ec(){
        return "forward:ec/homeUrl";
    }

    @RequestMapping("ec/loginUrl")
    public String loginUrl(HttpServletRequest request, Map<String, Object> map) {

        String error = "";
        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
        if (IncorrectCredentialsException.class.getName().equals(

                exceptionClassName) || AuthenticationException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if (DisabledAccountException.class.getName().equals(exceptionClassName) || LockedAccountException.class.getName().equals(exceptionClassName)) {
            error = "该用户被禁止！";
        } else if (exceptionClassName != null) {
            error = "其他错误：" + exceptionClassName;
        }

        map.put("error",error);
        return "forward:/ec/toLogin";
    }

    @RequestMapping("ec/toLogin")
    public ModelAndView toLogin(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("shiro/login");
        return modelAndView;
    }

    @RequestMapping("ec/homeUrl")
    public ModelAndView homeUrl(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("home/homePage");
        return modelAndView;
    }

    @RequestMapping("ec/signUpUrl")
    public String signUpUrl(){
        return "shiro/signUp";
    }
}
